package com.foreknow.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.Constanttype;
import com.foreknow.service.ConstantTypeService;
import com.foreknow.utils.Constance;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ConstantType")
public class ConstantTypeController {
    @Autowired
    private ConstantTypeService constantTypeService;
    @RequestMapping(value = "/findConstanttypeByPage",method = RequestMethod.POST)
    @ResponseBody
    public String findConstanttypeByPage(@RequestBody Map<String,Object> para) throws JsonProcessingException {
        //设置分页
        int pagenum = (int) para.get("pagenum");
        int pagesize = (int) para.get("pagesize");
        PageHelper.startPage(pagenum,pagesize);
        String query = (String) para.get("query");
        List<Constanttype> list =constantTypeService.searchAll(query);
        PageInfo<Constanttype> pageInfo =new PageInfo<>(list);
        //定义储存信息的集合
        HashMap<String,Object> hs =new HashMap<>();
        hs.put("data", pageInfo.getList());
        hs.put("total",pageInfo.getTotal());
        hs.put("meta", new Constance("msg", 200));
        ObjectMapper objectMapper = new ObjectMapper();
        if (list!=null){
            if (query!=null&& !query.equals("")){
                hs.put("data", pageInfo.getList());
                hs.put("total",pageInfo.getTotal());
                hs.put("meta",new Constance("查询常数类别成功",200));
            }else {
                hs.put("data",pageInfo);
                hs.put("meta",new Constance("获取常数类别列表成功",200));
            }
        }else {
            hs.put("meta",new Constance("获取常数类别列表失败",404));
        }
        return objectMapper.writeValueAsString(hs);
    }
//    添加常数类
    @RequestMapping(value = "/Insert", method = RequestMethod.POST)
    @ResponseBody
    public String Insert(@RequestBody Constanttype constanttype) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        int a =constantTypeService.insertConstantType(constanttype);
        if (a == 1){
            hs.put("meta",new Constance("添加成功",200));
        }else {
            hs.put("meta",new Constance("添加失败",500));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    @RequestMapping(value = "/selectConstanttype", method = RequestMethod.GET)
    @ResponseBody
    public String selectConstanttype(@RequestParam Map<String,Object> param) throws JsonProcessingException {
        String id = (String) param.get("id");
        Constanttype constanttype  = constantTypeService.selectById(Integer.parseInt(id));
        HashMap<String, Object> hs = new HashMap<>();
        if (constanttype != null){
            hs.put("data",constanttype);
            hs.put("meta",new Constance("获取常数类别成功",200));
        }else {
            hs.put("meta",new Constance("获取常数类别失败",500));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    //    修改常数类
    @RequestMapping(value = "/updateConstanttype", method = RequestMethod.POST)
    @ResponseBody
    public String updateConstanttype(@RequestBody Constanttype constanttype) throws JsonProcessingException {
        int a =constantTypeService.updaterConstantType(constanttype);

        HashMap<String, Object> hs = new HashMap<>();
        if (a == 1){
            hs.put("meta",new Constance("修改成功",200));
        }else {
            hs.put("meta",new Constance("修改失败",500));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    //    删除常数类
    @RequestMapping(value = "/deleteConstanttype", method = RequestMethod.GET)
    @ResponseBody
    public String deleteConstanttype(@RequestParam Integer id) throws JsonProcessingException {
        int a =constantTypeService.deleteConstantType(id);
        HashMap<String, Object> hs = new HashMap<>();
        if (a == 1){
            hs.put("meta",new Constance("删除成功",200));
        }else {
            hs.put("meta",new Constance("删除失败",500));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
}
